﻿<my:ChromelessWindow x:Class="MainWindow"
                     x:Name="RootWindow"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                     xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
                     xmlns:my="clr-namespace:SimpleFMMatrix"
                     xmlns:vm="clr-namespace:SimpleFMMatrix.ViewModel;assembly=SimpleFMMatrix.ViewModel"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
                     Title="MainWindow" Height="600" Width="1024" CaptionHeight="80">
    <Window.DataContext>
        <vm:SimpleFMMatrixViewModel />
    </Window.DataContext>
    <i:Interaction.Behaviors>
        <vm:PlayByKeyboardBehavior MatrixVM="{Binding Path=MatrixVM}" />
    </i:Interaction.Behaviors>
    <Grid x:Name="RootGrid" DataContext="{Binding Path=MatrixVM}" Margin="{Binding Path=ResizeBorderThickness, ElementName=RootWindow}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <TextBlock Text="Simple FM Matrix" Margin="24,6" FontSize="32" FontWeight="Light" />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal" Margin="1">
                    <Border VerticalAlignment="Top" BorderThickness="0.5" BorderBrush="#40000000">
                        <Grid Width="256" Height="50">
                            <Path x:Name="WaveformPath" Data="{Binding PreviewCurve}"
                                  Stroke="#C0000000" ClipToBounds="True" />
                            <Rectangle VerticalAlignment="Center" Height="0.5" Fill="#40000000" />
                            <Rectangle HorizontalAlignment="Center" Width="0.5" Fill="#40000000" />
                        </Grid>
                    </Border>
                    <Border VerticalAlignment="Top" BorderThickness="0.5" BorderBrush="#40000000">
                        <Grid Width="256" Height="50">
                            <Path x:Name="SpectrumPath" Data="{Binding PreviewSpectrum}"
                                  Stroke="#C0000000" Fill="#C0000000" ClipToBounds="True" />
                            <Rectangle VerticalAlignment="Center" Height="0.5" Fill="#40000000" />
                        </Grid>
                    </Border>
                </StackPanel>
                <my:TitlebarButtons Width="144" Height="32" VerticalAlignment="Top" />
            </StackPanel>
        </Grid>
        <Grid Grid.Row="1" Margin="0,20">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <StackPanel Margin="12,0.5" VerticalAlignment="Bottom">
                <ItemsControl DataContext="{Binding Path=ModulationIndices}" ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <my:MatrixEntry Value="{Binding Path=Value, Mode=TwoWay}"
                                                        IsSelfFeedBack="{Binding IsSelfFeedBack}"
                                                        OperatorName="{Binding Path=OperatorName}" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
                <Rectangle Height="12" />
                <ItemsControl DataContext="{Binding Path=OutputLevels}" ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <my:MatrixEntry Value="{Binding Path=Value, Mode=TwoWay}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
            <Rectangle Grid.Column="1" HorizontalAlignment="Left" Width="0.25" Fill="Black" />
            <StackPanel Grid.Column="1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="48" />
                        <ColumnDefinition Width="96" />
                        <ColumnDefinition Width="96" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="1" FontWeight="Light" FontSize="24" Margin="12,0,0,0" Foreground="#80000000" Text="Ratio" />
                    <TextBlock Grid.Column="2" FontWeight="Light" FontSize="24" Margin="12,0,0,0" Foreground="#80000000" Text="Offset" />
                    <TextBlock Grid.Column="3" FontWeight="Light" FontSize="24" Margin="12,0,0,0" Foreground="#80000000" Text="Envelope" />
                </Grid>
                <ItemsControl ItemsSource="{Binding Path=Operators}" ClipToBounds="True">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="6,3">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="48" />
                                    <ColumnDefinition Width="96" />
                                    <ColumnDefinition Width="96" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{Binding Path=OperatorName}"
                                           VerticalAlignment="Center" TextAlignment="Center"
                                           Foreground="#80000000" FontWeight="Light" FontSize="32" />
                                <my:FrequencyNumberView Grid.Column="1" Value="{Binding Path=Ratio.Value, Mode=TwoWay}" />
                                <my:FrequencyNumberView Grid.Column="2" Value="{Binding Path=Offset.Value, Mode=TwoWay}" />
                                <Canvas Grid.Column="3" Margin="12,0.5" Height="50">
                                    <Grid DataContext="{Binding Path=EnvelopeVM}">
                                        <my:EnvelopeZoomThumb DataContext="{Binding Path=DataContext, ElementName=RootGrid}"
                                                              Value="{Binding Path=EnvelopeHZoom.Value, Mode=TwoWay}" />
                                        <StackPanel Height="{Binding Path=Height.Value}"
                                                    Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
                                            <StackPanel.Resources>
                                                <Style TargetType="{x:Type my:EnvelopePointLevel}">
                                                    <Setter Property="Panel.ZIndex" Value="2" />
                                                </Style>
                                                <Style TargetType="{x:Type my:EnvelopePhaseDuration}">
                                                    <Setter Property="Panel.ZIndex" Value="1" />
                                                </Style>
                                                <Style TargetType="{x:Type my:EnvelopePhaseCurve}">
                                                    <Setter Property="Panel.ZIndex" Value="0" />
                                                </Style>
                                            </StackPanel.Resources>
                                            <my:EnvelopePointLevel Value="{Binding Path=KeyOnLevel.Value, Mode=TwoWay}" />
                                            <my:EnvelopePhaseCurve ClosedCurve="{Binding Path=AttackCurves.ClosedCurve}"
                                                                   Curve="{Binding Path=AttackCurves.Curve}"
                                                                   Increment="{Binding Path=AttackCurves.ValueIncrement}"
                                                                   Width="{Binding Path=AttackCurves.CurveWidth}"
                                                                   Slope="{Binding Path=AttackSlope.Value, Mode=TwoWay}" />
                                            <my:EnvelopePhaseDuration HZoom="{Binding Path=HZoom.Value}"
                                                                      Value="{Binding Path=AttackDuration.Value, Mode=TwoWay}" />
                                            <my:EnvelopePointLevel Value="{Binding Path=PeakLevel.Value, Mode=TwoWay}" />
                                            <my:EnvelopePhaseCurve ClosedCurve="{Binding Path=DecayCurves.ClosedCurve}"
                                                                   Curve="{Binding Path=DecayCurves.Curve}"
                                                                   Increment="{Binding Path=DecayCurves.ValueIncrement}"
                                                                   Width="{Binding Path=DecayCurves.CurveWidth}"
                                                                   Slope="{Binding Path=DecaySlope.Value, Mode=TwoWay}" />
                                            <my:EnvelopePhaseDuration HZoom="{Binding Path=HZoom.Value}"
                                                                      Value="{Binding Path=DecayDuration.Value, Mode=TwoWay}" />
                                            <my:EnvelopePointLevel Value="{Binding Path=SustainLevel.Value, Mode=TwoWay}" />
                                            <my:EnvelopePhaseCurve ClosedCurve="{Binding Path=ReleaseCurves.ClosedCurve}"
                                                                   Curve="{Binding Path=ReleaseCurves.Curve}"
                                                                   Increment="{Binding Path=ReleaseCurves.ValueIncrement}"
                                                                   Width="{Binding Path=ReleaseCurves.CurveWidth}"
                                                                   Slope="{Binding Path=ReleaseSlope.Value, Mode=TwoWay}" />
                                            <my:EnvelopePhaseDuration HZoom="{Binding Path=HZoom.Value}"
                                                                      Value="{Binding Path=ReleaseDuration.Value, Mode=TwoWay}" />
                                            <my:EnvelopePointLevel Value="{Binding Path=KeyOffLevel.Value, Mode=TwoWay}" />
                                        </StackPanel>
                                    </Grid>
                                </Canvas>
                                <Rectangle Grid.Column="3" VerticalAlignment="Bottom" Height="0.25" Fill="Black" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </Grid>
        <StackPanel Grid.Row="2" Margin="15,12.5">
            <StackPanel HorizontalAlignment="Center" Orientation="Horizontal" UseLayoutRounding="True">
                <Button Template="{DynamicResource MidiFileButton}" ToolTip="Convert a MIDI file"
                        Command="{Binding Path=ConvertMidiFileCommand}" CommandParameter="{x:Reference RootWindow}">
                    <Path Data="{StaticResource MidiFileIcon}" Fill="Black" Width="60" Height="60" />
                </Button>
                <Control Width="12" />
                <Border BorderThickness="0.5" BorderBrush="#C0000000">
                    <ItemsControl DataContext="{Binding PianoKeys}" ItemsSource="{Binding}" UseLayoutRounding="False">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <my:PianoKeyControl IsBlackKey="{Binding Path=PianoKey.IsBlackKey}"
                                                    XShift="{Binding Path=PianoKey.DisplayXShift}"
                                                    IsPressed="{Binding Path=IsPressed, Mode=TwoWay}" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemContainerStyle>
                            <Style TargetType="{x:Type ContentPresenter}">
                                <Setter Property="Panel.ZIndex" Value="{Binding Path=PianoKey.DisplayZIndex}" />
                            </Style>
                        </ItemsControl.ItemContainerStyle>
                    </ItemsControl>
                </Border>
            </StackPanel>
        </StackPanel>
    </Grid>
</my:ChromelessWindow>
